package com.yanqu.road.server.cmd.activity.curio;

import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.cangbao.CurioProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.curio.CurioMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.sync.message.SyncResponse;

import java.util.concurrent.CompletableFuture;

@Cmd(code = Protocol.S_CURIO_WATCH_ITEM, desc = "藏品查看（查他人）")
public class CurioWatchItemCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        CurioProto.CurioWatchPlayerItemReqMsg reqMsg = CurioProto.CurioWatchPlayerItemReqMsg.parseFrom(packet.getBytes());

        CurioProto.CurioWatchPlayerItemRespMsg.Builder clientMsg;
        if(reqMsg.getServerId() == GameServer.getInstance().getServerId()){
            clientMsg = CurioMgr.watchItem(reqMsg.getUserId(), reqMsg.getItemId());
            player.sendPacket(Protocol.U_CURIO_WATCH_ITEM, clientMsg);
        }else {
            clientMsg = CurioProto.CurioWatchPlayerItemRespMsg.newBuilder();
            CompletableFuture<SyncResponse> responseFuture = GamePlayerMgr.sendToGameServer(reqMsg.getServerId(), GameProtocol.S_CURIO_SERVICE_WATCH_OTHER_ITEM, player.getUserId(), reqMsg.toBuilder(), clientMsg);
            responseFuture.thenAccept(syncResponse -> {
                if (syncResponse.getErrorCode() == 0) {
                    player.sendPacket(Protocol.U_CURIO_WATCH_ITEM, clientMsg);
                }
            });
        }

    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            player.sendPacket(Protocol.U_CURIO_WATCH_ITEM, CurioProto.CurioWatchPlayerItemRespMsg.newBuilder().setRet(ret));
        }
    }
}
